/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    adc.c
  * @brief   This file provides code for the configuration
  *          of the ADC instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "adc.h"

/* USER CODE BEGIN 0 */
#include "usart.h"
#include "main.h"
#include "oled.h"
#include "gpio.h"
#include <stdio.h>

/* USER CODE END 0 */

ADC_HandleTypeDef hadc1;

/* ADC1 init function */
void MX_ADC1_Init(void)
{

  /* USER CODE BEGIN ADC1_Init 0 */

  /* USER CODE END ADC1_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC1_Init 1 */

  /* USER CODE END ADC1_Init 1 */

  /** Common config
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = ENABLE;
  hadc1.Init.NbrOfDiscConversion = 1;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 3;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_2;
  sConfig.Rank = ADC_REGULAR_RANK_2;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_3;
  sConfig.Rank = ADC_REGULAR_RANK_3;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */
//	HAL_ADC_Start (&hadc1);

  /* USER CODE END ADC1_Init 2 */

}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(adcHandle->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspInit 0 */

  /* USER CODE END ADC1_MspInit 0 */
    /* ADC1 clock enable */
    __HAL_RCC_ADC1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**ADC1 GPIO Configuration
    PA1     ------> ADC1_IN1
    PA2     ------> ADC1_IN2
    PA3     ------> ADC1_IN3
    */
    GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN ADC1_MspInit 1 */

  /* USER CODE END ADC1_MspInit 1 */
  }
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{

  if(adcHandle->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspDeInit 0 */

  /* USER CODE END ADC1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_ADC1_CLK_DISABLE();

    /**ADC1 GPIO Configuration
    PA1     ------> ADC1_IN1
    PA2     ------> ADC1_IN2
    PA3     ------> ADC1_IN3
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);

  /* USER CODE BEGIN ADC1_MspDeInit 1 */

  /* USER CODE END ADC1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */
uint32_t ADC_Valua = 0;    
float ADC_Value[3] = {0};

void ADC_Test(void)
{
   //ADC单通道采集,CubeMx中使能连续转换模式的.
//	if(HAL_ADC_PollForConversion (&hadc1,100)==HAL_OK)
//	{
//			ADC_Valua = HAL_ADC_GetValue (&hadc1);
//	}
//	printf("AD_Value =  %d \r\n",ADC_Valua);
//	
////	  HAL_ADC_Stop(&hadc1 );

  //ADC1 多通道采集,CubeMX中使能 间断转换模式的.
  for(uint8_t i=0;i<3;i++)
	{
			HAL_ADC_Start (&hadc1);
	if(HAL_ADC_PollForConversion (&hadc1,10)==HAL_OK)
	{
			ADC_Value[i] = HAL_ADC_GetValue (&hadc1);
	}

	}

}





//利用读到的ADC值计算热敏传感器的阻值
float resistance_trans(float  ADC_value)
{
	//标准化：根据电压电阻之间的关系来进行计算.
//	float voltage;
//	float Rt;
//	float R1 = 10.0f;  //10k的电阻,即热敏电阻RTC与R1电阻串联进行分压.
//	
//	voltage = ADC_value/4095.0f * 3.3f;
//	Rt = R1 * (3.3f/voltage-1.0f);    // Rt= R1*(Vcc/Vadc-1)，公式中-1，是基于分压规律而来.
//	return Rt;
	
	
	//根据 所读ADC:4095 = NTC电阻：100(假设NTC最大阻值为100)
	float Rt;
	Rt = ADC_value * 100 /4095;   //NTC电阻,温度高,阻值低.
	return Rt;
	
}

void GM_Value_OLED_Test(void)       
{
	OLED_ShowStr(0,1,(unsigned char*)"GM_Value:", 2);
	OLED_ShowValue(74,1,ADC_Value[0],2);
}



void Rt_OLED_Test(void)
{
//	float a = resistance_trans(ADC_Valua);
	float a = resistance_trans(ADC_Value[1]);
//	printf("Resistance_Value =  %.1f \r\n",a);
	OLED_ShowStr(0,3,(unsigned char*)"RM_Rt:", 2);
	OLED_ShowValue(50,3,a,2);
	if(ADC_Value[1]<15){
	HAL_GPIO_WritePin (Buzzer_GPIO_Port,Buzzer_Pin,GPIO_PIN_SET);
	}
}


void HW_OLED_Test(void)
{
	OLED_ShowStr(0,5,(unsigned char*)"HW_Value:", 2);
	OLED_ShowValue(74,5,ADC_Value[2],2);
  if(ADC_Value[2]< 1500){
	 HAL_GPIO_TogglePin(GPIOC,RED1_Pin);
		HAL_Delay (100);
		HAL_GPIO_TogglePin(GPIOC,RED2_Pin);
		HAL_Delay (100);
		HAL_GPIO_TogglePin(GPIOC,RED3_Pin);
		HAL_Delay (100);
	
	}
}
/* USER CODE END 1 */
